﻿Imports System.Xml.Serialization
Imports System.Text.RegularExpressions

Public Class SmsConfig
    Inherits DefaultConfig
    Private Shared mConfigInfo As New SmsInfo
    Private Shared mConfigFilePath As String
    Private Shared mFileDateTime As DateTime


    REM 配置文件路径
    Public Overloads Shared ReadOnly Property ConfigFilePath() As String
        Get
            If String.IsNullOrEmpty(mConfigFilePath) OrElse Not IO.File.Exists(mConfigFilePath) Then

                Dim Path As String = AppSettings("ConfigPath")
                If Not Path.EndsWith("/") Then Path = String.Concat(Path, "/")
                mConfigFilePath = String.Format("{0}sms.config", Path)

                If Not System.Web.HttpContext.Current Is Nothing Then
                    mConfigFilePath = System.Web.HttpContext.Current.Server.MapPath(mConfigFilePath)
                End If

            End If
            Return mConfigFilePath
        End Get
    End Property

    Public Shared Function Load() As SmsInfo
        Return Load(False)
    End Function

    Public Shared Function Load(ByVal checktime As Boolean) As SmsInfo
        mErrMessage = Nothing
        Try
            mConfigInfo = CType(LoadConfig(mFileDateTime, ConfigFilePath, mConfigInfo, checktime), SmsInfo)

        Catch ex As Exception
            mErrMessage = ex.Message
            mConfigInfo = New SmsInfo
        End Try
        Return mConfigInfo
    End Function

    Public Shared Sub Save()
        SaveConfig()
    End Sub

    Shared Sub New()

    End Sub
End Class

<Serializable()> <XmlRoot("SmsSetup")> Public Class SmsInfo
    Implements IConfigInfo

    Public CheckInteval As Integer

    Public SendInteval As Integer

    Private CurrentName As String


    Public ReadOnly Property Account As SmsAccount
        Get
            If Accounts Is Nothing OrElse Accounts.Length = 0 Then
                Return Nothing
            End If
            If String.IsNullOrEmpty(selected) Then
                Return Accounts(0)
            End If
            Return Find(selected)
        End Get
    End Property

    <XmlAttribute("selected")> Public selected As String
    <XmlElement("Account")> Public Accounts() As SmsAccount = {}


    Public Function Find(ByVal Name As String) As SmsAccount
        CurrentName = Name
        If Accounts Is Nothing OrElse Accounts.Length = 0 Then Return Nothing
        Return Array.Find(Accounts, AddressOf FindWhere)
    End Function

    Private Function FindWhere(ByVal item As SmsAccount) As Boolean
        Return item.Name = CurrentName
    End Function

    Protected Overrides Sub Finalize()
        'Accounts = Nothing
        MyBase.Finalize()
    End Sub
End Class

<Serializable()> Public Class SmsErr
    <XmlAttribute("code")> Public code As Integer
    <XmlText()> Public message As String
End Class

<Serializable()> Public Class SmsAccount
    Public Name As String
    Public Url As String

    ''' <summary>
    ''' 字符串 
    ''' </summary>
    ''' <param name="c">分隔符,默认&amp;</param>
    ''' <returns>a=1&amp;b=2&amp;c=3</returns>
    ''' <remarks></remarks>
    Public Function toUrlParam(Optional ByVal c As Char = CChar("&")) As String
        Dim s As String = String.Empty
        If Param.Length = 0 Then
            Return s
        End If
        For i As Integer = 0 To Param.Length - 1
            s = String.Concat(s, c, Param(i).name, "=", Web.HttpContext.Current.Server.UrlEncode(Replace(Param(i).Value, c, "")))
        Next
        Return s
    End Function
    ''' <summary>
    ''' 返回错误信息
    ''' </summary>
    ''' <param name="err">错误代码</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function ErrMessage(ByVal err As String) As String
        For Each se As SmsErr In mError
            If IsNumeric(err) AndAlso se.code = CInt(err) Then
                Return se.message
            End If
        Next
        Return err.ToString()
    End Function
    <XmlAttribute("target")> Public Target As String
    <XmlAttribute("action")> Public Action As String
    <XmlAttribute("content")> Public Content As String
    <XmlElement("Param")> Public Param() As ParamItem = {}    '其他附加配置的参数
    <XmlElement("Err")> Public mError() As SmsErr = {}
End Class




